<template>
  <div class="grade w1200">
    <div class="grade_title">
      <h3>已学习{{scoreList.length}}学期</h3>
      <span>共{{countTotalCurriculum}}学科</span>
    </div>

    <div class="grade_box">
      <el-card
        v-for="(tableData, idx) in scoreList"
        :key="idx"
        class="grade_item"
        :body-style="{ padding: '18px 15px'}"
      >
        <div class="grade_item_title">第{{idx+1 | num2Cn}}学期（{{tableData.length}}科）</div>
        <score-chart :setRef="'chart' + idx" :chartData="getChartData(tableData)"/>
        <el-table class="grade_table" :data="tableData" max-height="280px">
          <el-table-column
            prop="subject_name"
            label="学科名称"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            label="在线成绩"
            width="80"
            align="center"
            show-overflow-tooltip>
            <template slot-scope="scope">
              {{scope.row.onlineScore|mathFloor}}
            </template>
          </el-table-column>
          <el-table-column
            label="期末成绩"
            width="80"
            align="center"
            show-overflow-tooltip>
            <template slot-scope="scope">
              {{scope.row.endScore|mathFloor}}
            </template>
          </el-table-column>
          <el-table-column
            label="总成绩"
            width="65"
            align="center"
            show-overflow-tooltip>
            <template slot-scope="scope">
              {{scope.row.totalScore|mathFloor}}
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
  </div>
</template>

<script>
import ScoreChart from './comp/ScoreChart'
// eslint-disable-next-line no-unused-vars
import scoreListMock from './scoreListMock'
import {parseScoreList} from './dataHandle'

export default {
  name: 'Score',
  components: {
    ScoreChart
  },
  computed: {
    // 总学科数
    countTotalCurriculum() {
      return this.scoreList.reduce((pre, cur) => pre + cur.length, 0)
    }
  },
  data() {
    return {
      // 学生成绩列表
      scoreList: []
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    async init() {
      // const scoreList = scoreListMock
      const scoreList = await this.getScoreList()
      // 对第二层数据进行处理
      this.scoreList = parseScoreList(scoreList)
    },
    /**
     * 获取echart数据
     * @param tableData
     * @returns {{notPassCount: number, passCount: number}}
     */
    getChartData(tableData) {
      return {
        passCount: tableData.filter(s => s.totalScore >= 60).length,
        notPassCount: tableData.filter(s => s.totalScore < 60).length
      }
    },
    // 获取成绩列表
    getScoreList() {
      return this.$http.post('/subject/student_score_list', {
        major_id: this.$store.state.studentInfo.major_detail.major_id,
        grade: this.$store.state.studentInfo.grade,
      }).then(res => {
        return res || []
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .grade_title {
    margin-top: 40px;
    padding-left: 13px;
    margin-left: 21px;
    border-left: 4px solid #9dd521;

    display: flex;
    align-items: center;

    font-size: 13px;

    h3 {
      color: #222222;
      margin-right: 23px;
    }

    span {
      color: #999999;
    }
  }

  .grade_box {
    margin: 30px 0;
    display: flex;
    align-items: flex-start;
    flex-wrap: wrap;

    .grade_item {
      width: calc(100% / 3 - 26px);
      height: 556px;
      margin-bottom: 30px;

      &:not(:nth-child(3n)) {
        margin-right: 35px;
      }
    }
  }

  .grade_table {
    margin-top: 25px;
    border-radius: 8px;
    border: 1px solid #EBEBEB;
  }

</style>

<style lang="less">
  .grade_table {
    td, th.is-leaf {
      border-bottom: unset;
    }

    .el-table__header {
      th {
        background-color: #FBF9FB;
      }
    }
  }
</style>
